package pers.cl.dubbo.consumer.service.impl;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import pers.cl.dubbo.api.model.User;
import pers.cl.dubbo.api.service.CallBackListenerImpl;
import pers.cl.dubbo.api.service.UserService;
import pers.cl.dubbo.consumer.service.HelloService;

import java.util.concurrent.ExecutionException;

/**
 * @author chenlei
 * @date 2020/1/9
 * @description
 **/
@Service
public class HelloServiceImpl implements HelloService {

    @Reference(check = false,group = "user",validation = "true",timeout = 10000)
    private UserService userService;

    @Override
    public String sayHello(String msg) {
        User user = new User();
        user.setId("1");
        user.setUserName("sdfs");
        try{
            userService.addListener("helloWorld", new CallBackListenerImpl());
            String s = userService.sayHello(user);
            return s;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }

    @Override
    public String receiveAsync(String msg) {
        User user = new User();
        user.setId("1");
        user.setUserName("sdfs");
        try {
            return userService.async(user).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}
